/**
 * Project Name:smcs-mass-dns-ipcheck
 * File Name:CommonUtil.java
 * Package Name:com.surfilter.massdata.ipcheck.util
 * Date:2015-3-18上午09:07:07
 * Copyright (c) 2015, Shenzhen Surfilter Network Technology Co.,Ltd All Rights Reserved.
 * 
*/

package com.surfilter.massdata.spark.util.ipcheck;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.logging.Log;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.lib.output.MultipleOutputs;

import com.surfilter.massdata.spark.bean.ipcheck.ErrorIpSeg;
import com.surfilter.massdata.spark.bean.ipcheck.IpBean;
import com.surfilter.massdata.spark.bean.ipcheck.ResponseObj;


/**
 * ClassName:CommonUtil <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:   TODO ADD REASON. <br/>
 * Date:     2015-3-18 上午09:07:07 <br/>
 * @author   sunkai
 * @version  
 * @since    JDK 1.7
 * @see      
 */
public class CommonUtil {
	
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
	static String date = sdf.format(new Date());

	
	/**
	 * 获取uuid
	 * @return
	 */
	public static String getUUID(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	public static ResponseObj mergeConflictIpSeg(ErrorIpSeg newErrorIpSeg, long newIp, Long conflictQsip, Long conflictZzip, ErrorIpSeg errorIpSeg) throws IOException, InterruptedException{
		if(conflictQsip == -1l && conflictZzip == -1l){
			conflictQsip = newIp;
			conflictZzip = newIp;
			errorIpSeg = newErrorIpSeg;
		}else{
			if(isTheSameConflictSeg(newErrorIpSeg, errorIpSeg) && newIp == conflictZzip + 1){
				conflictZzip = newIp;
			}else if(isTheSameConflictSeg(newErrorIpSeg, errorIpSeg) && newIp == conflictQsip - 1){
				conflictQsip=newIp;
			}else{
				NullWritable nw = NullWritable.get();
				errorIpSeg.setQsip(conflictQsip);
				errorIpSeg.setZzip(conflictZzip);
//				if(errorIpSeg.getStorageType().equals(IpCheckConstants.BEIAN)){
//					mos.write("ipcheck", new Text(errorIpSeg.toString()), nw, StorageType.BEIAN_CONFLICT.toString());
//				}else{
//					mos.write("ipcheck", new Text(errorIpSeg.toString()), nw, StorageType.UPLOAD_CONFLICT.toString());
//				}
				conflictQsip = newIp;
				conflictZzip = newIp;
				errorIpSeg = newErrorIpSeg;
			}
		}
		ResponseObj obj = new ResponseObj();
		obj.setResult(false);
		obj.setConflictQsip(conflictQsip);
		obj.setConflictZzip(conflictZzip);
		obj.setErrorIpSeg(errorIpSeg);
		return obj;
		
	}
	
	public static boolean isTheSameConflictSeg(ErrorIpSeg newErrorIpSeg, ErrorIpSeg errorIpSeg){
		if(newErrorIpSeg != null && errorIpSeg != null
				&& newErrorIpSeg.getBbdws().equals(errorIpSeg.getBbdws())
				&& newErrorIpSeg.getConflictType().equals(errorIpSeg.getConflictType())
				&& newErrorIpSeg.getRegionIds().equals(errorIpSeg.getRegionIds())
				&& newErrorIpSeg.getStorageType().equals(errorIpSeg.getStorageType())
		)
			return true;
		return false;
	}
	
	public static boolean isEmpty(final String str){
		return str == null || str.length() == 0 || "".equals(str.trim()) || str.equalsIgnoreCase("null");
	}
	
	public static boolean isTheSame(IpBean bean1, IpBean bean2){
		
		if(CommonUtil.isEmpty(bean1.getHouseCode()) && !CommonUtil.isEmpty(bean2.getHouseCode())
			|| !CommonUtil.isEmpty(bean1.getHouseCode()) && CommonUtil.isEmpty(bean2.getHouseCode())
		    || !bean1.getHouseCode().equals(bean2.getHouseCode())
		){
			return false;
		}
		
		if(bean1.getIplx() != null && bean2.getIplx() == null
			|| bean1.getIplx() == null && bean2.getIplx() != null
			|| !bean1.getIplx().equals(bean2.getIplx())
		){
			return false;
		}
		
		if(bean1.getSfgn() != null && bean2.getSfgn() == null
			|| bean1.getSfgn() == null && bean2.getSfgn() != null
		    || !bean1.getSfgn().equals(bean2.getSfgn())
		){
			return false;
		}
			
		if(bean1.getSyfs() != null && bean2.getSyfs() == null
		    || bean1.getSyfs() == null && bean2.getSyfs() != null
		    || !bean1.getSyfs().equals(bean2.getSyfs())
		){
			return false;
		}
		
		if(bean1.getYmtzfwq() != null && bean2.getYmtzfwq() == null
		    || bean1.getYmtzfwq() == null && bean2.getYmtzfwq() != null
		    || !bean1.getYmtzfwq().equals(bean2.getYmtzfwq())
		){
			return false;
		}
			
		if(CommonUtil.isEmpty(bean1.getDwmc()) && !CommonUtil.isEmpty(bean2.getDwmc())
			|| !CommonUtil.isEmpty(bean1.getDwmc()) && CommonUtil.isEmpty(bean2.getDwmc())
		    || !bean1.getDwmc().equals(bean2.getDwmc())
		){
			return false;
		}
			
		if(bean1.getDwxz() != null && bean2.getDwxz() == null
			|| bean1.getDwxz() == null &&  bean2.getDwxz() != null
			|| !bean1.getDwxz().equals(bean2.getDwxz())
		){
			return false;
		}
		
		if(bean1.getDwxzjb() != null && bean2.getDwxzjb() == null
			|| bean1.getDwxzjb() == null && bean2.getDwxzjb() != null
			|| !bean1.getDwxzjb().equals(bean2.getDwxzjb())
		){
			return false;
		}
		
		if(bean1.getDwfl() != null && bean2.getDwfl() == null
			|| bean1.getDwfl() == null && bean2.getDwfl() != null
			|| !bean1.getDwfl().equals(bean2.getDwfl())
		){
			return false;
		}
		
		if(bean1.getDwhyfl() != null && bean2.getDwhyfl() == null
			|| bean1.getDwhyfl() == null && bean2.getDwhyfl() != null
			|| !bean1.getDwhyfl().equals(bean2.getDwhyfl())
		){
			return false;
		}
		
		if(CommonUtil.isEmpty(bean1.getJyxkzh()) && !CommonUtil.isEmpty(bean2.getJyxkzh())
			|| !CommonUtil.isEmpty(bean1.getJyxkzh()) && CommonUtil.isEmpty(bean2.getJyxkzh())
		    || !bean1.getJyxkzh().equals(bean2.getJyxkzh())
		){
			return false;
		}
		
		if(bean1.getShengid() != null && bean2.getShengid() == null
			|| bean1.getShengid() == null && bean2.getShengid() != null
			|| !bean1.getShengid().equals(bean2.getShengid())
		){
			return false;
		}
		
		if(bean1.getShiid() != null && bean2.getShiid() == null
			|| bean1.getShiid() == null && bean2.getShiid() != null
			|| !bean1.getShiid().equals(bean2.getShiid())
		){
			return false;
		}
		
		if(bean1.getXianid() != null && bean2.getXianid() == null
			|| bean1.getXianid() == null && bean2.getXianid() != null
			|| !bean1.getXianid().equals(bean2.getXianid())
		){
			return false;
		}
		
		if(bean1.getWgip() != null && bean2.getWgip() == null
			|| bean1.getWgip() == null && bean2.getWgip() != null
			|| !bean1.getWgip().equals(bean2.getWgip())
		){
			return false;
		}
		
		if(CommonUtil.isEmpty(bean1.getWgdz()) && !CommonUtil.isEmpty(bean2.getWgdz())
			|| !CommonUtil.isEmpty(bean1.getWgdz()) && CommonUtil.isEmpty(bean2.getWgdz())
		    || !bean1.getWgdz().equals(bean2.getWgdz())
		){
			return false;
		}
		
		return true;
	}
	
	
}
